//
//  PolyvVodPlayerViewManager.m
//  PolyvVodRnDemo
//
//  Created by 李长杰 on 2019/2/15.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "RNTPolyvVodManager.h"
#import "RNTPolyvVod.h"
#import <PLVVodSDK/PLVVodSDK.h>
#import <React/RCTUIManager.h>
#import <React/UIView+React.h>
NSString * NSStringFromPolyvVodRnErrorr(PolyvVodConfigRnErrorCodee code) {
  switch (code) {
    case PolyvVodRnError_Success:
      return @"成功";
    case PolyvVodRnError_NoVodKey:
      return @"vodKey为空";
    case PolyvVodRnError_NoDecodeKey:
      return @"decodeKey为空";
    case PolyvVodRnError_NoDecodeIv:
      return @"decodeIv为空";
    case PolyvVodRnError_NoViewerId:
      return @"viewerId为空";
    default:
      return @"";
  }
}

@interface RNTPolyvVodManager()

@property (nonatomic, weak) NSDictionary<NSNumber *,UIView *> *viewRegistry;

@end

@implementation RNTPolyvVodManager
{
  RNTPolyvVod *wrapper;
}
@synthesize bridge = _bridge;

RCT_EXPORT_MODULE();

RCT_EXPORT_VIEW_PROPERTY(play_parameters, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(onPlayVodEvent, RCTBubblingEventBlock);

- (UIView *)view
{
  if (wrapper == nil) {
    wrapper = [[RNTPolyvVod alloc] init];
    [wrapper setup];
  }
  return wrapper;
}
- (dispatch_queue_t)methodQueue
{
  return dispatch_get_main_queue();
}
// 参数 vodKey (必填)
// 参数 decodeKey (必填)
// 参数 decodeIv（必填）
// 参数 viewerId（必填）
// 参数 nickName（选填）
RCT_EXPORT_METHOD(initPolyVod:(NSString *)vodKey
                  decodeKey:(NSString *)decodeKey
                  decodeIv:(NSString *)decodeIv
                  viewerId:(NSString *)viewerId
                  nickName:(NSString *)nickName
                  findEventsWithResolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject
                  )
{
  RCTLog(@"config() - %@ 、 %@ 、 %@", vodKey, decodeKey, decodeIv);
  
  PolyvVodConfigRnErrorCodee errorCode = PolyvVodRnError_Success;
  if (!vodKey.length) {
    errorCode = PolyvVodRnError_NoVodKey;
  } else if (!decodeKey.length) {
    errorCode = PolyvVodRnError_NoDecodeKey;
  } else if (!decodeIv.length) {
    errorCode = PolyvVodRnError_NoDecodeIv;
  } else if (!viewerId.length) {
    errorCode = PolyvVodRnError_NoViewerId;
  }
  
  if (errorCode == PolyvVodRnError_Success) {
    
    NSError *error = nil;
    PLVVodSettings *settings = [PLVVodSettings settingsWithConfigString:vodKey key:decodeKey iv:decodeIv error:&error];
    NSString *readToken = settings.readtoken;
    
    settings.logLevel = PLVVodLogLevelAll;
    
    settings.viewerId = viewerId;
    settings.viewerName = nickName ? nickName : @"游客";
    
    NSDictionary *dic = @{ @"code": @(PolyvVodRnError_Success), @"token":readToken, @"isSign":@(NO) };
    
    resolve(dic);
  } else {
    NSString *errorDesc = NSStringFromPolyvVodRnErrorr(errorCode);
    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:errorCode userInfo:@{NSLocalizedDescriptionKey:errorDesc}];
    RCTLog(@"%@", errorDesc);
    reject([@(errorCode) stringValue], errorDesc, error);
  }
}

RCT_EXPORT_METHOD(updateVid:(NSString *)vid)
{
  RCTLog(@"updateVid vid=%@", vid);
  if (wrapper) {
    [wrapper updateVid:vid];
  }
}

RCT_EXPORT_METHOD(startOrPause
                  )
{
  RCTLog(@"startOrPause");
  if (wrapper) {
    [wrapper startOrPause];
  }
}

RCT_EXPORT_METHOD(play)
{
  RCTLog(@"play");
  if (wrapper) {
    [wrapper start];
  }
}

RCT_EXPORT_METHOD(pause)
{
  RCTLog(@"pause");
  if (wrapper) {
    [wrapper pause];
  }
}

RCT_EXPORT_METHOD(freePlayer)
{
  RCTLog(@"release");
  if (wrapper) {
    [wrapper destroyPlayer];
    wrapper = nil;
    
  }
       
  
}

RCT_EXPORT_METHOD(setFullScreen:(BOOL)fullScreen)
{
  if (fullScreen) {
    RCTLog(@"setFullScreen YES");
  } else {
    RCTLog(@"setFullScreen NO");
  }
  if (wrapper) {
    [wrapper setFullScreen:fullScreen];
  }
}

@end
